<?php
class Zend_View_Helper_Paging
{
    /**
     * View instance
     *
     * @var Zend_View_Instance
     */
    public $view;
    
    /**
     * Paging
     * @param string $module
     * @param string $controller
     * @param string $action
     * @param array  $params
     * @param int    $total
     * @param int    $page
     * @param int    $limit
     * @param int    $page_size
     * @param string $style
     */
    public function paging($baseUrl, $module, $controller, $action, $params, $total=0, $page=0, $limit=30, $page_size=8, $style='page_item')
    {
        //Start paging
        $query = '';
        $querystring = '';
        $paging = '';
        
        if(!empty($params) && is_array($params))
        {
            //Add query
            foreach($params as $key => $value)
            {            	
                $querystring .= '/'.$key.'/'.urlencode($value);
            }
        }

        //Check total
        if($total>0)
        {
            //Current page number
            $total_pages = ceil($total/$limit);
            $start   = max($page-intval($page_size/2), 1);
            $end     = ($total>$limit)? $start + $page_size -1 : $total;
            
            $from_rs = ($page-1)*$limit+1;
            $to_rs   = $from_rs+$limit-1;
            $to_rs   = ($to_rs>$total) ? $total : $to_rs;
            //Add total            
            $paging  = '<div class="paging grey '.$style.'"><div class="wrap">';	
          	$paging  .= '<div class="total">Có <b>'.$total.'</b> kết quả thích hợp </div>';	
          	$paging  .= '<div class="pageOn">';
            if($total>$limit)
            {
                //Start
                if($start > 1)
                {
                    $i = 1;
                    $query = $baseUrl.'/'.$module.'/'.$controller.'/'.$action.'/page/'.$i.$querystring;
                    $paging .= '<span class="first"><a href="'.$query.'">'.$i.'</a></span>';
                }
                
                //Previous
                if($page > 1)
                {
                    $i = $page-1;
                    $query = $baseUrl.'/'.$module.'/'.$controller.'/'.$action.'/page/'.$i.$querystring;                             
                    $paging .= '<span class="prev"><a href="'.$query.'">Trở về</a></span>';
                }
                                
                //Loop
                for($i = $start; $i <= $end && $i <= $total_pages; $i++)
                {
                	
                    if($i == $page)
                    {                    	
                        $paging .= '<span class="active">'.$i.'</span>';
                    }
                    else
                    {
                        $query = $baseUrl.'/'.$module.'/'.$controller.'/'.$action.'/page/'.$i.$querystring;
                        $paging .= '<span><a href="'.$query.'">'.$i.'</a></span>';                        
                    }
                }

               

                //Check total pages
                if($page < $total_pages)
                {
                    $i = $page + 1;
                    $query = $baseUrl.'/'.$module.'/'.$controller.'/'.$action.'/page/'.$i.$querystring;                    
                    $paging .= '<span class="next"><a href="'.$query.'">Tiếp theo</a></span>';
                }
                
                //Check total pages
                if($total_pages > $end)
                {
                    $i = $total_pages;
                    $query = $baseUrl.'/'.$module.'/'.$controller.'/'.$action.'/page/'.$i.$querystring;
                    $paging .= '<span class="last"><a href="'.$query.'">'.$i.'</a></span>';
                }
            }           
            $paging .= '<span class="total">(Có <b>'.$total.'</b> kết quả)</span></div></div></div>';
        }
        return $paging;
    }
    
    /**
     * Sets the view instance.
     *
     * @param  Zend_View_Interface $view View instance
     * @return Zend_View_Helper_Paging
     */
    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
    }
  
}